<!--
 * @Author: your name
 * @Date: 2020-06-02 15:30:11
 * @LastEditTime: 2020-07-06 15:55:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \IM_Web\im_web\src\components\chatMain\chatMain.vue
--> 
<template>
    <div class="flex">
        <div class style="width:360px;">
            <ImList :sessionsData="sessionsList" @goInSession="goInSession" />
        </div>
        <div class style="width:780px;">
            <ChatRegion
                :account="account"
                :chatName="chatName"
                :sessionId="sessionId"
                @getHistoryMsgs="getHistoryMsgs"
                :userInfos="userInfos"
                :myInfo="myInfo"
            />
        </div>
    </div>
</template>

<script>
import ImList from "../imList/imlist";
import ChatRegion from "../chatRegion/chatRegion";
import util from "../../utils";
import config from "../../configs";
import tea from "../../utils/TEA.js";
// import SDK from "../../js/js/NIM_Web_NIM_v7.6.0.js";
// let nim = null;
export default {
    props: {},
    data() {
        return {
            account: "",
            lastSessionId: "",
            sessionId: "",
            scene: "",
            myGroupIcon: config.defaultGroupIcon,
            myAdvancedIcon: config.defaultAdvancedIcon,
            chatName: ""
        };
    },
    computed: {
        sessionsList() {
            let sessionlist = this.$store.state.sessionlist.filter(item => {
                console.log(item);
                console.log(util);
                let lastMsg = item.lastMsg || {};
                if (lastMsg.type === "text") {
                    // item.lastMsgShow = lastMsg.text || "";
                    item.lastMsgShow =
                        tea.xxtea_decrypt(lastMsg.text, config.teaKey) || "";
                } else if (lastMsg.type === "custom") {
                    item.lastMsgShow = util.parseCustomMsg(lastMsg);
                } else if (
                    lastMsg.scene === "team" &&
                    lastMsg.type === "notification"
                ) {
                    item.lastMsgShow = util.generateTeamSysmMsg(lastMsg);
                } else if (util.mapMsgType(lastMsg)) {
                    item.lastMsgShow = `[${util.mapMsgType(lastMsg)}]`;
                } else {
                    item.lastMsgShow = "";
                }
                if (item.updateTime) {
                    item.updateTimeShow = util.formatDate(
                        item.updateTime,
                        true
                    );
                }
                console.log(item);
                return item;
            });
            return sessionlist;
            // console.log(this.$store.state.sessionlist);
            // let arr = this.$store.state.sessionlist;
            // arr = arr.reverse();
            // return arr;
        },
        myInfo() {
            return this.$store.state.myInfo;
        },
        userInfos() {
            return this.$store.state.userInfos;
        }
    },
    created() {},
    mounted() {
        // setTimeout(() => {
        //     this.account = "account";
        // }, 10000);
    },
    destroy() {
        this.$store.dispatch("resetCurrSession");
    },
    watch: {},
    methods: {
        goInSession({ ...params }) {
            let p = params;
            console.log(p);
            this.account = p.account;
            this.sessionId = p.sessionId;
            this.scene = p.scene;
            this.chatName = p.name;
            this.$store.dispatch("setCurrSession", params.sessionId);
            console.log(this.$store.state.currSessionId);
            this.$store.commit("resetSessionUnread", p.sessionId);
            // if (this.sessionId !== this.$store.state.currSessionId) {

            // }
        },
        getHistoryMsgs() {
            // if (this.canLoadMore) {
            this.$store.dispatch("getHistoryMsgs", {
                scene: this.scene,
                to: this.account,
                sessionId: this.sessionId
            });
            // }
        }
    },
    components: {
        ImList,
        ChatRegion
    }
};
</script>

<style scoped lang="less">
</style>
